package com.fs.mapper;

import com.fs.entity.Cart;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface CartMapper {

    /**/
    @Select("SELECT * FROM cart WHERE user_id = #{userId} AND product_id = #{productId} LIMIT 1")
    Cart getCartByUserAndProduct(@Param("userId") Long userId, @Param("productId") Long productId);

    /*添加购物车*/
    @Insert("INSERT INTO cart (user_id, product_id, product_name, product_image, price, quantity, checked, create_time, update_time) " +
            "VALUES (#{userId}, #{productId}, #{productName}, #{productImage}, #{price}, #{quantity}, #{checked}, #{createTime}, #{updateTime})")
    void insertCart(Cart cart);

    /*更新购物车*/
    @Update("UPDATE cart SET quantity = #{quantity}, update_time = #{updateTime} WHERE id = #{id}")
    void updateCart(Cart cart);

    /*查询购物车*/
    @Select("SELECT * FROM cart WHERE user_id = #{userId} ORDER BY update_time DESC")
    List<Cart> listByUserId(@Param("userId") Long userId);

}
